<?php 

class Admin_Helper_ItemAdjustment extends Zend_Controller_Action_Helper_Abstract{
	public function preDispatch(){
		$request = $this->getActionController()->getRequest();
        if($request->isPost() && $request->getPost('adjustItem')) {
            $orderItem = Doctrine_Model_OrderItems::getOrderItem($request->getPost('orderItemsId'));
            
            //adjustment algorithm eventually shift to a function;
            $adjustmentValue = $request->getPost('adjustmentValue') * 100; // value in cents
            if($adjustmentValue < 0){
            	$adjustmentValue = ($orderItem[0]['price'] <= abs($adjustmentValue))?($orderItem[0]['price'] * -1):$adjustmentValue;
            }

            //make adjustment
            $result = Doctrine_Model_ItemAdjustments::adjustItem( $request->getPost(), $adjustmentValue);
            $result = Doctrine_Model_OrderItems::setItemStatus($request->getPost('orderItemsId'), 2); //itemStatus[2] = adjusted
        	$result = Doctrine_Model_Orders::changeStatus($request->getPost('orderId'), 4); // status[4] = under_review
            $result = Doctrine_Model_Orders::adjustTotal($request->getPost('orderId'));
            
        }
	}
}